package com.apexsoft.assets.base.utils;

import cn.hutool.core.lang.Validator;
import com.apexsoft.assets.base.exception.ValidException;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class ValidateUtil {

    /**
     * 验证该字符串是否是数字
     *
     * @param str 字符串内容
     * @return  boolean 是否是数字
     * @author  zhuzongdong
     * @date  2021/4/16
     */
    public static void isNumber(String str) throws ValidException {
        if (!Validator.isNumber(str)) {
            Map<String, String> error = new HashMap<>();
            error.put("数据校验异常", "输入的必须是数字");
            throw new ValidException(error);
        }
    }

    /**
     *  字符串数字是否在指定范围
     *
     * @param str 字符串
     * @param min 最小值
     * @param max 最大值
     * @return  boolean
     * @author  zhuzongdong
     * @date  2021/4/16
     */
    public static void isBetween(String str,int min ,int max) throws ValidException{
        ValidateUtil.isNumber(str);
        if(!Validator.isBetween(Integer.valueOf(str),min,max)){
            Map<String, String> error = new HashMap<>();
            error.put("数据校验异常","输入的数字必须在["+min+","+max+"]区间内");
            throw new ValidException(error);
        }
    }

    public static void isBetween(int i,int min ,int max) throws ValidException{
            if(!Validator.isBetween(i,min,max)){
                Map<String, String> error = new HashMap<>();
                error.put("数据校验异常","输入的数字必须在["+min+","+max+"]区间内");
                throw new ValidException(error);
            }
    }

    /**
     * 验证字符串是否为日期
     *
     * @param str 字符串
     * @param pattern 格式
     * @return  boolean
     * @author  zhuzongdong
     * @date  2021/4/16
     */
    public static void isDate(String str,String pattern) throws ValidException{
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try{
            Date date =sdf.parse(str);
            if(!str.equals(sdf.format(date))){
                throw new Exception();
            }
        }catch (Exception e){
            Map<String, String> error = new HashMap<>();
            error.put("数据校验异常","输入的字符串不符合指定日期格式");
            throw new ValidException(error);
        }
    }

    /**
     * 验证数字是否为日期
     *
     * @param number 数字
     * @param pattern 格式
     * @return  boolean
     * @author  zhuzongdong
     * @date  2021/4/16
     */
    public static void isDate(Number number,String pattern) throws ValidException{
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try{
            Date date =sdf.parse(String.valueOf(number));
            if(!String.valueOf(number).equals(sdf.format(date))){
                throw new Exception();
            }
        }catch (Exception e){
            Map<String, String> error = new HashMap<>();
            error.put("数据校验异常","输入的内容不符合指定日期格式");
            throw new ValidException(error);
        }
    }

    public static void isNotNull(Object obj)throws ValidException{
        List<Field> fieldList = new LinkedList<>();
        Class tempClass = obj.getClass();
        while (tempClass != null){
            fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            tempClass = tempClass.getSuperclass();
        }
        for (Field field : fieldList){
            ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
            if(apiModelProperty != null){
                boolean required = apiModelProperty.required();
                if(required){
                    Object value = null;
                    try {
                        field.setAccessible(true);
                        value = field.get(obj);
                    } catch (IllegalAccessException e) {
                        log.error("Validate isNotNull ERROR: ", e);
                    }
                    if(value == null){
                        Map<String, String> error = new HashMap<>();
                            error.put("数据校验异常","字段："+field.getName()+"不能为空");
                            throw new ValidException(error);
                    }else if(value instanceof String){
                        if(((String) value).length() == 0){
                            Map<String, String> error = new HashMap<>();
                            error.put("数据校验异常","字段："+field.getName()+"不能为空");
                            throw new ValidException(error);
                        }
                    }
                }
            }
        }
    }
}
